﻿using System;
using System.Data;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// DA_Comment 的摘要说明。
    /// </summary>
    public class CommentDal
    {
        #region 录入评论
        /// <summary>
        /// 录入评论
        /// </summary>
        /// <param name="model"></param>
        public void Add(Comment model)
        {
            using (Entities db = new Entities())
            {
                if (model.ReplyId != null)
                {
                    var info = db.Comments.FirstOrDefault(x => x.Id == model.ReplyId);
                    if (info != null)
                    {
                        info.MaxFloor = Convert.ToInt32(info.MaxFloor) + 1;
                        info.UpdateTime = DateTime.Now;
                        model.Floor = info.MaxFloor;
                        model.Path = info.Path;
                        db.Comments.Add(model);
                        db.SaveChangesAsync().Wait();

                        model.Path = info.Path + model.Id + "|";
                        db.SaveChangesAsync().Wait();
                    }
                }
                else
                {
                    model.MaxFloor = 1;
                    model.Floor = 1;
                    model.Path = string.Format("|{0}|", model.Id);
                    db.Comments.Add(model);
                    db.SaveChangesAsync().Wait();
                }



            }
        } 
        #endregion

        #region 删除评论
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.Comments.Where(x => ids.Contains(x.Id));
                foreach (var v in q)
                {
                    db.Comments.Remove(v);
                }
                db.SaveChangesAsync().Wait();
            }
        } 
        #endregion

        #region 删除评论
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public IList<int> DeleteForIP(string ip)
        {
            using (Entities db = new Entities())
            {
                var ids = (from x in db.Comments where x.IP == ip select x.ContentId).ToArray();

                db.Database.ExecuteSqlCommand("delete from CMS_Comments where IP={0}", ip);

                return ids;
            }
        } 
        #endregion

        #region 审核评论
        /// <summary>
        /// 审核评论
        /// </summary>
        /// <param name="ids"></param>
        public void Pass(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.Comments.Where(x => ids.Contains(x.Id));
                foreach (var v in q)
                {
                    v.Pass = !v.Pass;
                }
                db.SaveChangesAsync().Wait();
            }
        } 
        #endregion

        #region 显示评论
        /// <summary>
        /// 显示评论－－前台使用
        /// </summary>
        /// <param name="top"></param>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public IList<Comment> GetList(int top, int contentId)
        {
            using (Entities db = new Entities())
            {
                var q = from x in db.Comments
                        where contentId == x.ContentId && x.Pass
                        orderby x.Id descending
                        select x;
                return q.Take(top).ToArray();
            }
        } 
        #endregion

        #region 绑定分页数据
        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="pass"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Comment> BindList(int contentId, bool pass, PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                var query = (from x in db.Comments
                             select x);
                if (contentId > 0)
                {
                    query = query.Where(x => x.ContentId == contentId);
                }

                if (pass)
                {
                    query = query.Where(x => x.Pass == pass);
                }

                pp.RecordCount = query.Count();
                query = query.OrderByDescending(x => x.UpdateTime).Skip(pp.Skip).Take(pp.PageSize);

                return query.ToArray();
            }
        } 
        #endregion

        #region 本内容下最热评论
        /// <summary>
        /// 本内容下最热评论
        /// </summary>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public IList<Comment> HotComments(int contentId)
        {
            using (Entities db = new Entities())
            {
                var query = (from x in db.Comments
                             where x.ContentId == contentId
                             orderby (x.Support + x.Oppose) descending
                             select x);

                return query.ToArray();
            }
        } 
        #endregion

        #region 支持或者反对
        /// <summary>
        /// 支持或者反对
        /// </summary>
        /// <param name="id"></param>
        /// <param name="support"></param>
        public Comment SupportOrOppose(int id, bool support)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                db.Configuration.LazyLoadingEnabled = false;
                Comment info = db.Comments.FirstOrDefault(x => x.Id == id);
                if (info == null)
                {
                    return null;
                }

                if (support)
                {
                    info.Support = info.Support + 1;
                }
                else
                {
                    info.Oppose = info.Oppose + 1;
                }
                db.SaveChangesAsync().Wait();

                return info;
            }
        }
        #endregion

        #region 获取自己和上级
        /// <summary>
        /// 获取自己和上级
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public IList<Comment> GetFatherAndMe(string path)
        {
            int[] ids = path.ToArrInt();
            using (Entities db = new Entities())
            {
                return db.Comments.Where(x => ids.Contains(x.Id)).OrderBy(x => x.Id).ToArray();
            }
        }
        #endregion
    }
}